//
//  word2vec.h
//  word2vec
//
//  Created by LazyLie on 14-8-11.
//  Copyright (c) 2014年 NLP Group. All rights reserved.
//

#ifndef word2vec_word2vec_h
#define word2vec_word2vec_h

#include "huffman.h"
#include <vector>
#include <map>
#include <mutex>

class word2vec {
public:
    typedef std::vector<double> vec_t;
    typedef std::vector< std::pair<std::string, vec_t> > result_t;
    
    word2vec();
    
    void init(std::string);
    void train();
    result_t get_result();
    
private:
    static const int MIN_WORD_APPEARENCE = 5, THREAD_NUM = 8;
    
    std::map<std::string, int> vocabulary;
    int vocabulary_size, vec_size, window_size, train_actual;
    huffman tree;
    std::vector<vec_t> vec_word, vec_tree;
    std::vector<int> material;
    std::vector<std::mutex> vec_word_mutex, vec_tree_mutex;
    std::mutex io_mutex;
    double alpha_start;

    void train_thread(int st, int ed);
    double sigmoid(double x);
    double dot(const vec_t &a, const vec_t &b);
    vec_t add(const vec_t &a, const vec_t &b);
    vec_t mul(double a, const vec_t &b);

};

#endif
